package slog

import (
	"log"
	"os"
	"sync"
)

/*
* @Auther:create by hjx
* @Email :1245863260@qq.com g1245863260@gmail.com
* @Date  :2021/4/5 18:44
 */

const (
	Info     = "[INFO]"
	Warning  = "[WARNING]"
	Error    = "[ERROR]"
	LogBegin = "Buddha bless hjx programs without bugs"
	LogEnd   = "Thank you for Buddha bless"
)

type SlogBese struct {
	LogName   string
	LogPrefix string
	Logger    *log.Logger
	File      *os.File
}

var once sync.Once
var Slog SlogBese

func SlogInit(logName, prefix string) {
	if Slog == (SlogBese{}) {
		once.Do(func() {
			file, err := os.OpenFile(logName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
			if err != nil {
				log.Fatalf("Can not open the %s log file,exit 2", logName)
			}
			Slog.LogName = logName
			Slog.LogPrefix = prefix
			Slog.Logger = log.New(file, prefix, log.LstdFlags|log.Lshortfile)
			Slog.File = file
		})
	}
}
